package com.transaction.common.util;

public class DataUtil {

    //后补空格
    public static String suffixReplenishSpace(String value, Integer colLength) {
        try {
            //TODO 长度超长需要截取


            while (value.getBytes(ConstUtil.FILE_ENCODING).length < colLength) {
                value += " ";
            }

            return value;
        }catch(Exception e){
            e.printStackTrace();
        }

        return value;
    }

    //补齐小数位
    public static String fillingDecimalPlaces(String value, Integer colDecimal) {
        value = (value == null ? "" : value);

        int decimalIndex = value.indexOf(".");
        if (decimalIndex != -1){
            String prefix = value.substring(0,decimalIndex);
            String suffix = value.substring(decimalIndex+1);
            suffix = suffixReplenishZero(suffix,colDecimal);
            value = prefix + suffix;
        }else{
            value += suffixReplenishZero(null,colDecimal);
        }
        return value;
    }


    /**
     * 对数值进行前补0操作
     * @Title: prefixReplenishZero
     * @author: dy.yin 2021/8/20 17:24
     */
    public static String prefixReplenishZero(String value,Integer length){
        value = (value == null ? "" : value);

        while(value.length() < length){
            value = "0" + value;
        }

        if(value.length() > length){
            value = value.substring(0,length);
        }

        return value;
    }

    /**
     * 对数值进行后补0操作
     * @Title: suffixReplenishZero
     * @author: dy.yin 2021/8/22 20:53
     */
    public static String suffixReplenishZero(String value,Integer length){
        value = (value == null ? "" : value);

        while(value.length() < length){
            value += "0";
        }

        if(value.length() > length){
            value = value.substring(0,length);
        }
        return value;
    }
}
